import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import React, { useRef, useEffect } from "react";
import classes from "./applePayBtn.module.less";
import { useExternal, useLockFn } from "ahooks";
import instance from "../../instance";
import { jsx as _jsx } from "react/jsx-runtime";
function ApplePayBtn(props) {
    var appleSdkStatus = useExternal("https://applepay.cdn-apple.com/jsapi/v1/apple-pay-sdk.js", {
        type: "js",
        keepWhenUnused: true
    });
    var status = useExternal(instance.payConfig.paypalUri, {
        type: "js",
        keepWhenUnused: true
    });
    var wrapper = useRef(null);
    var applepay = useRef();
    var onPay = useLockFn(/*#__PURE__*/ _async_to_generator(function() {
        var _ref, isEligible, countryCode, currencyCode, merchantCapabilities, supportedNetworks, paymentRequest, session;
        return _ts_generator(this, function(_state) {
            switch(_state.label){
                case 0:
                    return [
                        4,
                        applepay.current.config()
                    ];
                case 1:
                    _ref = _state.sent(), isEligible = _ref.isEligible, countryCode = _ref.countryCode, currencyCode = _ref.currencyCode, merchantCapabilities = _ref.merchantCapabilities, supportedNetworks = _ref.supportedNetworks;
                    paymentRequest = {
                        countryCode: countryCode,
                        currencyCode: "USD",
                        merchantCapabilities: merchantCapabilities,
                        supportedNetworks: supportedNetworks,
                        requiredBillingContactFields: [
                            "name",
                            "phone",
                            "email",
                            "postalAddress"
                        ],
                        requiredShippingContactFields: [],
                        total: {}
                    };
                    session = new window.ApplePaySession(4, paymentRequest);
                    session.onvalidatemerchant = function() {};
                    session.onpaymentmethodselected = function() {};
                    session.onpaymentauthorized = function() {};
                    session.oncancel = function() {
                        console.log("Apple Pay Cancelled !!");
                    };
                    session.begin();
                    return [
                        2
                    ];
            }
        });
    }));
    var setupApplePay = function() {
        var _ref = _async_to_generator(function() {
            var isEligible, e;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        _state.trys.push([
                            0,
                            3,
                            ,
                            4
                        ]);
                        if (!(window.paypal && window.ApplePaySession && wrapper.current)) return [
                            3,
                            2
                        ];
                        if (!(window.ApplePaySession.supportsVersion(4) && window.ApplePaySession.canMakePayments())) return [
                            3,
                            2
                        ];
                        applepay.current = window.paypal.ApplePay();
                        return [
                            4,
                            applepay.current.config()
                        ];
                    case 1:
                        isEligible = _state.sent().isEligible;
                        if (!isEligible) {
                            throw new Error("applepay is not eligible");
                        }
                        wrapper.current.innerHTML = '<apple-pay-button id="btn-appl" buttonstyle="black" type="buy" locale="en">';
                        document.getElementById("btn-app1").addEventListener("click", onPay);
                        _state.label = 2;
                    case 2:
                        return [
                            3,
                            4
                        ];
                    case 3:
                        e = _state.sent();
                        return [
                            3,
                            4
                        ];
                    case 4:
                        return [
                            2
                        ];
                }
            });
        });
        return function setupApplePay() {
            return _ref.apply(this, arguments);
        };
    }();
    useEffect(function() {
        if (appleSdkStatus === "ready" && status === "ready") {
            setupApplePay();
        }
    }, [
        appleSdkStatus,
        status
    ]);
    return /*#__PURE__*/ _jsx("div", {
        ref: wrapper,
        className: classes.wrap
    });
}
export default ApplePayBtn;

 //# sourceMappingURL=index.js.map